package com.yoomiito.app.share.ui

import android.content.Context
import android.graphics.Bitmap
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import cn.droidlover.xdroidmvp.net.progress.ProgressHelper
import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.google.zxing.WriterException
import com.yoomiito.app.R
import com.yoomiito.app.utils.ImageUtils
import com.yoomiito.app.utils.ResUtil
import com.yoomiito.app.utils.ZxingUtil
import java.util.logging.Handler

open class BaseShareView(open val context: Context){
    lateinit var mErWeiMaIv: ImageView//显示二维码
    lateinit var mAdView: View//分享图view
    lateinit var mShowADView: ImageView//展示生成的图片
    lateinit var mAdIv: LinearLayout//用于设置待生成图片的background
    var makeBitmapListener:(Bitmap)->Unit={}

    open fun setData(any: Any){}

    /**
     * 生成二维码
     */
    fun makeErWeiMa(url:String) {
        try {
            mErWeiMaIv.setImageBitmap(ZxingUtil.cretaeZxing(url, ResUtil.getColor(R.color.color_black)))
        } catch (e: WriterException) {
            e.printStackTrace()
        }
    }

    /**
     * 生成主图
     */
    fun makeThemeAd(url: String, defaultResource: Int) {
        val builder: RequestBuilder<Bitmap> = if (defaultResource == 0) {
            Glide.with(context).asBitmap().load(url)
        } else {
            Glide.with(context).asBitmap().load(defaultResource)
        }
        builder.into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                mAdIv.background = ImageUtils.bitmapToDrawable(resource)
                val mBitmap = ImageUtils.viewSaveToImage(mAdView)
                makeBitmapListener(mBitmap)
                mShowADView.setImageBitmap(mBitmap)
            }
        })
    }
}